<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
</head>
<body>
  <div id="test">
    FirstName: <input type="text" v-model="firstName"><br>
    LastName: <input type="text" v-model="lastName"><br>
    
    FullName(双向): <input type="text" v-model="fullName">
  </div>

  <script src="../js/vue.js"></script>
  <script>
    const vm = new Vue({
      el: "#test",
      data: {
        firstName: 'A',
        lastName: 'B',
        fullName4: 'A-B'
      },

      computed: {
        fullName: {
          // 初始执行第一次, 后面firstName或lastName变化时又会执行
          get () {
            console.log('get')
            return this.firstName + '-' + this.lastName
          },
          // fullName被修改时
          set (value) {  // C-D
            console.log('set')
            const names = value.split('-')
            // 更新firstName和lastName
            this.firstName = names[0]
            this.lastName = names[1]
          }
        }
      },
    })

  </script>
</body>
</html>